<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\DownloadModel;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminDownloadController extends AdminBaseController
{
    //资料下载


    //列表
    public function index(){
        $res = Db::name('download')
            ->order('id desc')
            ->paginate(10);
        $data = $res->toArray();
        $data = $data['data'];
        $page = $res->render();

        $this->assign('list',$data);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        if (!empty($data['file_names']) && !empty($data['file_urls'])) {
            $data['file_url'] = $data['file_urls'][0];
            $data['file_name'] = $data['file_names'][0];
            unset($data['file_names']);
            unset($data['file_urls']);
        }
        $downloadModel = new DownloadModel();
        $res = $downloadModel->create($data);
        if($res){
            $this->success('添加成功!', url('AdminDownload/index'));
        }else{
            $this->error('失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('download')
            ->where(['id'=>$id])
            ->find();
        $this->assign('post',$post);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        if (!empty($data['file_names']) && !empty($data['file_urls'])) {
            $data['file_url'] = $data['file_urls'][0];
            $data['file_name'] = $data['file_names'][0];
            unset($data['file_names']);
            unset($data['file_urls']);
        }
        $downloadModel = new DownloadModel();
        // 显式指定更新数据操作
        $res = $downloadModel->isUpdate(true)->save($data);
        if($res){
            $this->success('保存成功!', url('AdminDownload/index'));
        }else{
            $this->error('失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $downloadModel = new DownloadModel();
        $res = $downloadModel->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功!');
        }else{
            $this->error('失败');
        }
    }
}
